package mdm.led.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import mdm.led.dao.FiveDetailsMapper;
import mdm.led.dao.FiveItmeMapper;
import mdm.led.dao.FiveProcedureMapper;
import mdm.led.entity.five.FiveDetails;
import mdm.led.entity.five.FiveItme;
import mdm.led.entity.five.FiveProcedure;
import mdm.led.service.FiveProcedureService;
import mdm.led.util.ListToString;

/**
 *
 * @author :Monarch丿He
 * @date :2018年11月2日
 * @explain :流程管控 业务接口实现
 */
@Service("fiveProcedureServiceImpl")
public class FiveProcedureServiceImpl implements FiveProcedureService {

	@Autowired
	private FiveProcedureMapper fiveProcedureDao; // 流程表 Dao 接口
	@Autowired
	private FiveItmeMapper fiveItemDao; // 管控表项目 dao 接口
	@Autowired
	private FiveDetailsMapper fiveDetailsDao; // 管控详情表 Dao 接口

	@Override
	public boolean addFiveProcedure(FiveProcedure fiveProcedure) {
		// 标识
		Boolean iRet = false;
		List<FiveItme> list = fiveProcedure.getFiveItems();
		try {
			Date date = new Date();
			fiveProcedure.setSetTime(date);
			fiveProcedure.setEstate(1);
			int i = fiveProcedureDao.addFiveProcedure(fiveProcedure);
			if (i > 0) {

				for (FiveItme itme : list) {
					// 将流程表id 存入 检查项 对应 流程 ID
					itme.setProcedureId(fiveProcedure.getProcedureId());
					// 存库
					fiveItemDao.addFiveItme(itme);
				}
				iRet = true;
			}
		} catch (Exception e) {
			///
		}
		return iRet;
	}

	@Override
	public int updateFiveEstate(Integer id) {
		return fiveProcedureDao.updateFiveEstate(id);
	}

	@Override
	public List<FiveProcedure> findFiveProcedure(String name) {
		// 表集合
		List<FiveProcedure> tables = fiveProcedureDao.findFiveProcedure(name);
		Integer id = 0;
		Integer count = 0;
		// 循环查出 流程表子表的总数
		for (FiveProcedure item : tables) {
			id = item.getProcedureId();
			// 当前表 子表总数量
			count = fiveDetailsDao.findCountByProcedureId(id);
			// 给总数字段赋值
			item.setSonTableCount(count);
		}

		return tables;
	}

	@Override
	public FiveProcedure findFiveProcedureById(Integer id) {
		return fiveProcedureDao.findFiveProcedureById(id);
	}

	@Override
	public List<FiveDetails> findFiveDetailsList(String username, Integer procedureId) {

		List<FiveDetails> list = fiveDetailsDao.findFiveDetailsList(username, procedureId);

		return list;
	}

	@Override
	public FiveProcedure findProcedureByPidAndUid(Integer procedureId, Integer userId) {
		// 根据id 查询表格
		FiveProcedure fiveProcedure = fiveProcedureDao.findFiveProcedureById(procedureId);
		// 查询当前表格所有检查项
		List<FiveItme> items = fiveItemDao.findAllFiveItemById(procedureId);

		// 根据 用户id 和表格 Id 查询详情表
		FiveDetails details = fiveDetailsDao.findDetailsByPidAndUid(procedureId, userId);

		if (details != null) {
			// 读取详情表所有检查项目
			String detailsItems = details.getItemId();
			if(detailsItems !=null && detailsItems != ""){
				// 分割为检查项id 数组
				String[] itemsArr = detailsItems.split(",");
				// 所有检查项集合
				List<FiveItme> itemList = new ArrayList<FiveItme>();
				FiveItme e = null;
				// 循环解析当前 ID 字符串 ','
				for (String itemId : itemsArr) {
					// 取到一项
					e = fiveItemDao.findFiveItmeById(Integer.parseInt(itemId));
					// 添加到临时集合
					itemList.add(e);
				}
				details.setItems(itemList);
			}

		} else {
			// 实例化
			details = new FiveDetails();
			details.setProcedureId(fiveProcedure.getProcedureId());
			details.setWriteMan(userId);
			details.setCause(2);
			// 表所有检查项
			List<FiveItme> itemss = items;
			// 详情表所有检查项
			List<FiveItme> itemByDetails = new ArrayList<FiveItme>();
			FiveItme a = null;
			try {
				List<Integer> itemIds = new ArrayList<Integer>();
				for (FiveItme fiveItme : itemss) {
					fiveItme.setProcedureId(0);
					fiveItemDao.addFiveItme(fiveItme);
					itemIds.add(fiveItme.getFiveItem());
					a = fiveItemDao.findFiveItmeById(fiveItme.getFiveItem());
					itemByDetails.add(a);
				}
				String itemStr = ListToString.integerListToString(itemIds, ",");
				// 赋值检查项目字符串
				details.setItemId(itemStr);
				details.setItems(itemByDetails);
				details.setWriteTime(new Date());
				fiveDetailsDao.addFiveDetails(details);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		fiveProcedure.setFiveItems(items);
		fiveProcedure.setFiveDetails(details);

		return fiveProcedure;

	}

	@Override
	public boolean updateFiveDetails(FiveDetails fiveDetails) {
		// 据流程表 ID 和 填表人 ID查询详情表ID
		String detailsId = fiveDetailsDao.findDetailsByUidAndPid(fiveDetails.getProcedureId(),
				fiveDetails.getWriteMan());
		boolean iRet = false;
		if ((detailsId != null && detailsId != "")) {
			fiveDetailsDao.updateFiveDetailsCause(Integer.parseInt(detailsId));
			// 所有检查项
			List<FiveItme> items = fiveDetails.getItems();
			try {
				for (FiveItme item : items) {
					// 修改检查项信息
					fiveItemDao.updateFiveItem(item);
				}
				iRet = true;
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		/*
		 * else { // 所有检查项 List<FiveItme> items = fiveDetails.getItems();
		 * 
		 * try { List<Integer> itemIds = new ArrayList<Integer>(); for (FiveItme
		 * fiveItme : items) { fiveItemDao.addFiveItme(fiveItme);
		 * itemIds.add(fiveItme.getFiveItem()); } String itemStr =
		 * ListToString.integerListToString(itemIds, ","); // 赋值检查项目字符串
		 * fiveDetails.setItemId(itemStr);
		 * fiveDetailsDao.addFiveDetails(fiveDetails); iRet = true; } catch
		 * (Exception e) { e.printStackTrace(); } }
		 */

		return iRet;

	}

	@Override
	public FiveProcedure findProcedureById(Integer id) {
		// 根据id 查询表格
		FiveProcedure fiveProcedure = fiveProcedureDao.findFiveProcedureById(id);
		// 查询当前表格所有检查项
		List<FiveItme> items = fiveItemDao.findAllFiveItemById(id);
		fiveProcedure.setFiveItems(items);
		fiveProcedure.setFiveDetails(null);
		return fiveProcedure;
	}

	@Override
	public FiveDetails findFiveDetailsById(Integer id) {
		FiveDetails fiveDetails = fiveDetailsDao.findFiveDetailsByDetailsId(id);
		// 读取详情表所有检查项目
		String detailsItems = fiveDetails.getItemId();
		if(detailsItems != null && detailsItems != ""){
			// 分割为检查项id 数组
			String[] itemsArr = detailsItems.split(",");
			// 所有检查项集合
			List<FiveItme> itemList = new ArrayList<FiveItme>();
			FiveItme e = null;
			// 循环解析当前 ID 字符串 ','
			for (String itemId : itemsArr) {
				// 取到一项
				e = fiveItemDao.findFiveItmeById(Integer.parseInt(itemId));
				// 添加到临时集合
				itemList.add(e);
			}
			fiveDetails.setItems(itemList);
		}
		
		return fiveDetails;
	}

}
